# clearing up memory

rm(list = ls())

# setting wp working directory

setwd("~/Dropbox")

# reading data from EU Data portal website

M <- read.csv(url(
  "http://opendata.caceres.es/GetData/GetData?dataset=om:Farola&format=csv"), 
  header=TRUE)

# loading up required packages

library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag():    dplyr, stats
library(ggmap)
## Google Maps API Terms of Service: http://developers.google.com/maps/terms.
## Please cite ggmap if you use it: see citation("ggmap") for details.
# reading header and looking and summary and structure

head(M)
##                                                               uri
## 1     http://opendata.caceres.es/recurso/energia/farolas/Farola/1
## 2    http://opendata.caceres.es/recurso/energia/farolas/Farola/10
## 3   http://opendata.caceres.es/recurso/energia/farolas/Farola/100
## 4  http://opendata.caceres.es/recurso/energia/farolas/Farola/1000
## 5 http://opendata.caceres.es/recurso/energia/farolas/Farola/10000
## 6 http://opendata.caceres.es/recurso/energia/farolas/Farola/10001
##   om_funcionamiento om_proteccion om_tipoLuz om_materialS  geo_long
## 1                 S         FALSE         AC            F -6.381244
## 2                 S         FALSE          P            F -6.379596
## 3                 S         FALSE         AC            F -6.382879
## 4                 S         FALSE          F            F -6.369740
## 5                 S         FALSE          E            F -6.383445
## 6                 S         FALSE          E            F -6.383772
##   om_potencia om_tipoLampara  geo_lat om_altura om_tipoSoporte
## 1         250           VSAP 39.46876        10             Ba
## 2         400             HM 39.46820        10              O
## 3          70           VSAP 39.47023         5             Ba
## 4         125           VMCC 39.47953         3              C
## 5         250           VSAP 39.47094         5              C
## 6         250           VSAP 39.47087         5              C
##   om_circuitos om_materialL
## 1            3            P
## 2            1            P
## 3            2            P
## 4            3            F
## 5            2            P
## 6            2            P
str(M)
## 'data.frame':    15004 obs. of  13 variables:
##  $ uri              : Factor w/ 15004 levels "http://opendata.caceres.es/recurso/energia/farolas/Farola/1",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ om_funcionamiento: Factor w/ 2 levels "N","S": 2 2 2 2 2 2 2 2 2 2 ...
##  $ om_proteccion    : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
##  $ om_tipoLuz       : Factor w/ 7 levels "AA","AC","E",..: 2 7 2 5 3 3 3 3 3 3 ...
##  $ om_materialS     : Factor w/ 3 levels "F","Fi","Fr": 1 1 1 1 1 1 1 1 1 1 ...
##  $ geo_long         : num  -6.38 -6.38 -6.38 -6.37 -6.38 ...
##  $ om_potencia      : int  250 400 70 125 250 250 250 250 250 250 ...
##  $ om_tipoLampara   : Factor w/ 11 levels "F","FCBC","H",..: 10 4 10 9 10 10 10 10 10 10 ...
##  $ geo_lat          : num  39.5 39.5 39.5 39.5 39.5 ...
##  $ om_altura        : int  10 10 5 3 5 5 5 5 5 5 ...
##  $ om_tipoSoporte   : Factor w/ 4 levels "Ba","Br","C",..: 1 4 1 3 3 3 3 3 3 3 ...
##  $ om_circuitos     : int  3 1 2 3 2 2 2 2 2 2 ...
##  $ om_materialL     : Factor w/ 4 levels "Al","F","O","P": 4 4 4 2 4 4 4 4 4 4 ...
summary(M)
##                                                               uri       
##  http://opendata.caceres.es/recurso/energia/farolas/Farola/1    :    1  
##  http://opendata.caceres.es/recurso/energia/farolas/Farola/10   :    1  
##  http://opendata.caceres.es/recurso/energia/farolas/Farola/100  :    1  
##  http://opendata.caceres.es/recurso/energia/farolas/Farola/1000 :    1  
##  http://opendata.caceres.es/recurso/energia/farolas/Farola/10000:    1  
##  http://opendata.caceres.es/recurso/energia/farolas/Farola/10001:    1  
##  (Other)                                                        :14998  
##  om_funcionamiento om_proteccion   om_tipoLuz om_materialS
##  N:  187           Mode :logical   AA:  191   F :14975    
##  S:14817           FALSE:15004     AC:10156   Fi:   23    
##                    NA's :0         E : 1148   Fr:    6    
##                                    ER:  719               
##                                    F : 2009               
##                                    O :  260               
##                                    P :  521               
##     geo_long       om_potencia    om_tipoLampara     geo_lat     
##  Min.   :-6.424   Min.   : 11.0   VSAP   :10696   Min.   :39.44  
##  1st Qu.:-6.383   1st Qu.:125.0   VMCC   : 2776   1st Qu.:39.46  
##  Median :-6.375   Median :150.0   FCBC   :  499   Median :39.47  
##  Mean   :-6.376   Mean   :167.3   HM     :  331   Mean   :39.47  
##  3rd Qu.:-6.369   3rd Qu.:250.0   MC     :  268   3rd Qu.:39.48  
##  Max.   :-6.338   Max.   :400.0   VSBP   :  217   Max.   :39.51  
##                                   (Other):  217                  
##    om_altura      om_tipoSoporte  om_circuitos    om_materialL
##  Min.   : 1.000   Ba:8836        Min.   : 1.000   Al:  193    
##  1st Qu.: 5.000   Br:2584        1st Qu.: 1.000   F : 1882    
##  Median : 8.000   C :2816        Median : 2.000   O :   25    
##  Mean   : 7.408   O : 768        Mean   : 2.298   P :12904    
##  3rd Qu.:10.000                  3rd Qu.: 3.000               
##  Max.   :15.000                  Max.   :10.000               
## 
# renaming column headers from Spanish to English

colnames(M) <- c(
  "url", 
  "functioning", 
  "protection", 
  "lightType", 
  "materialS", 
  "long", 
  "power", 
  "lampsType", 
  "lat", 
  "height", 
  "supportType", 
  "circuits",
  "materialL")

# taking longitude and latitude values in a different data frame to extract map

z <- subset(M, select = c("long", "lat"))


# using another approach using Hadley's ggmap paper approach
# didnt use markers or path arguments

x <- get_googlemap('càceres', zoom=15, scale = 2)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=c%C3%A0ceres&zoom=15&size=640x640&scale=2&maptype=terrain
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=c%C3%A0ceres
y <- ggmap(x, extent = 'device')

# light Type

y + geom_point(data = M, 
               mapping = aes(x = long, y = lat, color = lightType), 
               size = 0.75)
## Warning: Removed 9618 rows containing missing values (geom_point).

# lamps Type

y + geom_point(data = M, 
               mapping = aes(x = long, y = lat, color = lampsType), 
               size = 0.75)
## Warning: Removed 9618 rows containing missing values (geom_point).

# height 

y + geom_point(data = M, 
               mapping = aes(x = long, y = lat, color = height), 
               size = 0.75) + 
  scale_colour_gradient(low = "blue", high = "red", space = "Lab")
## Warning: Removed 9618 rows containing missing values (geom_point).

# support type 

y + geom_point(data = M, 
               mapping = aes(x = long, y = lat, color = supportType), 
               size = 0.75)
## Warning: Removed 9618 rows containing missing values (geom_point).

# materialS

y + geom_point(data = M, 
               mapping = aes(x = long, y = lat, color = materialS), 
               size = 0.75)
## Warning: Removed 9618 rows containing missing values (geom_point).

# materialL

y + geom_point(data = M, 
               mapping = aes(x = long, y = lat, color = materialL), 
               size = 0.75)
## Warning: Removed 9618 rows containing missing values (geom_point).

# circuits

y + geom_point(data = M, 
               mapping = aes(x = long, y = lat, color = circuits), 
               size = 0.75) +
  scale_colour_gradient(low = "blue", high = "red", space = "Lab")
## Warning: Removed 9618 rows containing missing values (geom_point).

# functioning

y + geom_point(data = M, 
               mapping = aes(x = long, y = lat, color = functioning), 
               size = 0.75)
## Warning: Removed 9618 rows containing missing values (geom_point).

# protection

y + geom_point(data = M, 
               mapping = aes(x = long, y = lat, color = protection), 
               size = 0.75)
## Warning: Removed 9618 rows containing missing values (geom_point).